﻿<%@ WebHandler Language="C#" Class="UpdatePrice" %>

using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Data.Linq;
using System.Linq;
using System.Web;
using LdExpress;

public class UpdatePrice : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        int nPermissions = 0;
        if (context.Session["logined_id"] != null)
        {
            if (context.Session["Prices"] != "") nPermissions = int.Parse(context.Session["Prices"].ToString());
        }

        if (!(nPermissions > 3))
        {
            context.Response.Write(JsonConvert.SerializeObject("access denied!"));
        }

        string items = context.Request.Params["items"];
        if (!string.IsNullOrEmpty(items))
        {
            List<LdExpress.Prices> udpitems = JsonConvert.DeserializeObject<List<LdExpress.Prices>>(items);
            string ids = "";



            ids = string.Join(",", udpitems.Select(q => q.ID));

            //foreach (LdExpress.Prices item in udpitems)
            //{
            //    ids += item.ID + ",";
            //}
            System.Data.DataSet ds = new System.Data.DataSet();
            string condition = "";
            if (!string.IsNullOrEmpty(ids))
            {
                condition = " and ID in (" + ids + ")";
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject("not valid"));
            }
            int nNumOfPages = 0;
            LdExpress.Prices.GetList(condition, "", 999, 1, out nNumOfPages, out ds, LdExpress.GV.cn, LdExpress.GV.trans);
            Array arr = udpitems.ToArray();
            int i = 0;
            if (ds.Tables.Count > 0)
            {
                LdExpress.Prices price = new LdExpress.Prices();
                LdExpress.Prices newprice = new LdExpress.Prices();
                string ex = "_" + context.Session["logined_user"] + ": " + DateTime.Now.ToString("dd-MM-yyyy");
                string newEx = "";
                bool ischanged = false;
                foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                {
                    price = new LdExpress.Prices();

                    if (!dr.IsNull("ID")) price.ID = long.Parse(dr["ID"].ToString());
                    if (!dr.IsNull("DC20")) price.DC20 = dr["DC20"].ToString();
                    if (!dr.IsNull("DC40")) price.DC40 = dr["DC40"].ToString();
                    if (!dr.IsNull("HC40")) price.HC40 = dr["HC40"].ToString();
                    if (!dr.IsNull("RF20")) price.RF20 = dr["RF20"].ToString();
                    if (!dr.IsNull("RF40")) price.RF40 = dr["RF40"].ToString();
                    //if (!dr.IsNull("TSPort")) price.TSPort = dr["TSPort"].ToString();
                    //if (!dr.IsNull("Sailing")) price.Sailing = dr["Sailing"].ToString();
                    //if (!dr.IsNull("TT")) price.TT = dr["TT"].ToString();
                    if (!dr.IsNull("Validity")) price.Validity = dr["Validity"].ToString();
                    if (!dr.IsNull("ValidityEx")) price.ValidityEx = dr["ValidityEx"].ToString();
                    //if (!dr.IsNull("Import")) price.Import = dr["Import"].ToString();
                    if (!dr.IsNull("Description")) price.Description = dr["Description"].ToString();
                    if (!dr.IsNull("DescriptionEx")) price.DescriptionEx = dr["DescriptionEx"].ToString();
                    if (!dr.IsNull("CarrID")) price.CarrID = long.Parse(dr["CarrID"].ToString());
                    if (!dr.IsNull("PortToID")) price.PortToID = long.Parse(dr["PortToID"].ToString());
                    if (!dr.IsNull("PortFromID")) price.PortFromID = long.Parse(dr["PortFromID"].ToString());
                    if (!dr.IsNull("Sort")) price.Sort = int.Parse(dr["Sort"].ToString());
                    if (!dr.IsNull("UserID")) price.UserID = int.Parse(dr["UserID"].ToString());
                    if (!dr.IsNull("CarrName")) price.CarrName = dr["CarrName"].ToString();
                    if (!dr.IsNull("LastUpdate")) price.LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());

                    if (!dr.IsNull("OT20")) price.OT20 = dr["OT20"].ToString();
                    if (!dr.IsNull("OT40")) price.OT40 = dr["OT40"].ToString();
                    if (!dr.IsNull("FL20")) price.FL20 = dr["FL20"].ToString();
                    if (!dr.IsNull("FL40")) price.FL40 = dr["FL40"].ToString();
                    if (!dr.IsNull("CONT45")) price.CONT45 = dr["CONT45"].ToString();
                    if (!dr.IsNull("ValidityOTFL")) price.ValidityOTFL = dr["ValidityOTFL"].ToString();
                    if (!dr.IsNull("DescriptionOTFL")) price.DescriptionOTFL = dr["DescriptionOTFL"].ToString();

                    //set new value
                    newprice = udpitems.Where(q => q.ID == price.ID).FirstOrDefault();

                    if (newprice != null)
                    {
                        newprice.DC20 = HttpUtility.HtmlDecode(newprice.DC20);
                        if ((newprice.DC20 != "") && (newprice.DC20 != price.DC20.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.DC20.Split('_')[0];
                            if (price.DC20.Split('_').Length >= 2)
                                newEx = ex + "_" + price.DC20.Split('_')[1].Substring(0, price.DC20.Split('_')[1].Length) + " " + price.DC20.Split('_')[0];
                            price.DC20 = newprice.DC20 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.DC20 == "")
                        {
                            price.DC20 = newprice.DC20;
                            ischanged = true;
                        }
                        newprice.DC40 = HttpUtility.HtmlDecode(newprice.DC40);
                        if ((newprice.DC40 != "") && (newprice.DC40 != price.DC40.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.DC40.Split('_')[0];
                            if (price.DC40.Split('_').Length >= 2)
                                newEx = ex + "_" + price.DC40.Split('_')[1].Substring(0, price.DC40.Split('_')[1].Length) + " " + price.DC40.Split('_')[0];
                            price.DC40 = newprice.DC40 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.DC40 == "")
                        {
                            price.DC40 = newprice.DC40;
                            ischanged = true;
                        }

                        newprice.HC40 = HttpUtility.HtmlDecode(newprice.HC40);
                        if ((newprice.HC40 != "") && (newprice.HC40 != price.HC40.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.HC40.Split('_')[0];
                            if (price.HC40.Split('_').Length >= 2)
                                newEx = ex + "_" + price.HC40.Split('_')[1].Substring(0, price.HC40.Split('_')[1].Length) + " " + price.HC40.Split('_')[0];
                            price.HC40 = newprice.HC40 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.HC40 == "")
                        {
                            price.HC40 = newprice.HC40;
                            ischanged = true;
                        }

                        newprice.RF20 = HttpUtility.HtmlDecode(newprice.RF20);
                        if ((newprice.RF20 != "") && (newprice.RF20 != price.RF20.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.RF20.Split('_')[0];
                            if (price.RF20.Split('_').Length >= 2)
                                newEx = ex + "_" + price.RF20.Split('_')[1].Substring(0, price.RF20.Split('_')[1].Length) + " " + price.RF20.Split('_')[0];
                            price.RF20 = newprice.RF20 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.RF20 == "")
                        {
                            price.RF20 = newprice.RF20;
                            ischanged = true;
                        }

                        newprice.RF40 = HttpUtility.HtmlDecode(newprice.RF40);
                        if ((newprice.RF40 != "") && (newprice.RF40 != price.RF40.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.RF40.Split('_')[0];
                            if (price.RF40.Split('_').Length >= 2)
                                newEx = ex + "_" + price.RF40.Split('_')[1].Substring(0, price.RF40.Split('_')[1].Length) + " " + price.RF40.Split('_')[0];
                            price.RF40 = newprice.RF40 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.RF40 == "")
                        {
                            price.RF40 = newprice.RF40;
                        }

                        newprice.Validity = HttpUtility.HtmlDecode(newprice.Validity);
                        if ((newprice.Validity != "") && (newprice.Validity != price.Validity.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.Validity.Split('_')[0];
                            if (price.Validity.Split('_').Length >= 2)
                                newEx = ex + "_" + price.Validity.Split('_')[1].Substring(0, price.Validity.Split('_')[1].Length) + " " + price.Validity.Split('_')[0];
                            price.Validity = newprice.Validity + newEx;
                            ischanged = true;
                        }
                        else if (newprice.Validity == "")
                        {
                            price.Validity = newprice.Validity;
                            ischanged = true;
                        }

                        newprice.ValidityEx = HttpUtility.HtmlDecode(newprice.ValidityEx);
                        if ((newprice.ValidityEx != "") && (newprice.ValidityEx != price.ValidityEx.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.ValidityEx.Split('_')[0];
                            if (price.ValidityEx.Split('_').Length >= 2)
                                newEx = ex + "_" + price.ValidityEx.Split('_')[1].Substring(0, price.ValidityEx.Split('_')[1].Length) + " " + price.ValidityEx.Split('_')[0];
                            price.ValidityEx = newprice.ValidityEx + newEx;
                            ischanged = true;
                        }
                        else if (newprice.ValidityEx == "")
                        {
                            price.ValidityEx = newprice.ValidityEx;
                            ischanged = true;
                        }

                        newprice.Description = HttpUtility.HtmlDecode(newprice.Description);
                        if ((newprice.Description != "") && (newprice.Description != price.Description.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.Description.Split('_')[0];
                            if (price.Description.Split('_').Length >= 2)
                                newEx = ex + "_" + price.Description.Split('_')[1].Substring(0, price.Description.Split('_')[1].Length) + " " + price.Description.Split('_')[0];
                            price.Description = newprice.Description + newEx;
                            ischanged = true;
                        }
                        else if (newprice.Description == "")
                        {
                            price.Description = newprice.Description;
                            ischanged = true;
                        }

                        newprice.DescriptionEx = HttpUtility.HtmlDecode(newprice.DescriptionEx);
                        if ((newprice.DescriptionEx != "") && (newprice.DescriptionEx != price.DescriptionEx.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.DescriptionEx.Split('_')[0];
                            if (price.DescriptionEx.Split('_').Length >= 2)
                                newEx = ex + "_" + price.DescriptionEx.Split('_')[1].Substring(0, price.DescriptionEx.Split('_')[1].Length) + " " + price.DescriptionEx.Split('_')[0];
                            price.DescriptionEx = newprice.DescriptionEx + newEx;
                            ischanged = true;
                        }
                        else if (newprice.DescriptionEx == "")
                        {
                            price.DescriptionEx = newprice.DescriptionEx;
                            ischanged = true;
                        }
                        //OTFL
                        newprice.OT20 = HttpUtility.HtmlDecode(newprice.OT20);
                        if ((newprice.OT20 != "") && (newprice.OT20 != price.OT20.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.OT20.Split('_')[0];
                            if (price.OT20.Split('_').Length >= 2)
                                newEx = ex + "_" + price.OT20.Split('_')[1].Substring(0, price.OT20.Split('_')[1].Length) + " " + price.OT20.Split('_')[0];
                            price.OT20 = newprice.OT20 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.OT20 == "")
                        {
                            price.OT20 = newprice.OT20;
                            ischanged = true;
                        }
                        newprice.OT40 = HttpUtility.HtmlDecode(newprice.OT40);
                        if ((newprice.OT40 != "") && (newprice.OT40 != price.OT40.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.OT40.Split('_')[0];
                            if (price.OT40.Split('_').Length >= 2)
                                newEx = ex + "_" + price.OT40.Split('_')[1].Substring(0, price.OT40.Split('_')[1].Length) + " " + price.OT40.Split('_')[0];
                            price.OT40 = newprice.OT40 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.OT40 == "")
                        {
                            price.OT40 = newprice.OT40;
                            ischanged = true;
                        }
                        newprice.FL20 = HttpUtility.HtmlDecode(newprice.FL20);
                        if ((newprice.FL20 != "") && (newprice.FL20 != price.FL20.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.FL20.Split('_')[0];
                            if (price.FL20.Split('_').Length >= 2)
                                newEx = ex + "_" + price.FL20.Split('_')[1].Substring(0, price.FL20.Split('_')[1].Length) + " " + price.FL20.Split('_')[0];
                            price.FL20 = newprice.FL20 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.FL20 == "")
                        {
                            price.FL20 = newprice.FL20;
                            ischanged = true;
                        }
                        newprice.FL40 = HttpUtility.HtmlDecode(newprice.FL40);
                        if ((newprice.FL40 != "") && (newprice.FL40 != price.FL40.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.FL40.Split('_')[0];
                            if (price.FL40.Split('_').Length >= 2)
                                newEx = ex + "_" + price.FL40.Split('_')[1].Substring(0, price.FL40.Split('_')[1].Length) + " " + price.FL40.Split('_')[0];
                            price.FL40 = newprice.FL40 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.FL40 == "")
                        {
                            price.FL40 = newprice.FL40;
                            ischanged = true;
                        }

                        newprice.CONT45 = HttpUtility.HtmlDecode(newprice.CONT45);
                        if ((newprice.CONT45 != "") && (newprice.CONT45 != price.CONT45.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.CONT45.Split('_')[0];
                            if (price.CONT45.Split('_').Length >= 2)
                                newEx = ex + "_" + price.CONT45.Split('_')[1].Substring(0, price.CONT45.Split('_')[1].Length) + " " + price.CONT45.Split('_')[0];
                            price.CONT45 = newprice.CONT45 + newEx;
                            ischanged = true;
                        }
                        else if (newprice.FL40 == "")
                        {
                            price.CONT45 = newprice.FL40;
                            ischanged = true;
                        }

                        newprice.ValidityOTFL = HttpUtility.HtmlDecode(newprice.ValidityOTFL);
                        if ((newprice.ValidityOTFL != "") && (newprice.ValidityOTFL != price.ValidityOTFL.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.ValidityOTFL.Split('_')[0];
                            if (price.ValidityOTFL.Split('_').Length >= 2)
                                newEx = ex + "_" + price.ValidityOTFL.Split('_')[1].Substring(0, price.ValidityOTFL.Split('_')[1].Length) + " " + price.ValidityOTFL.Split('_')[0];
                            price.ValidityOTFL = newprice.ValidityOTFL + newEx;
                            ischanged = true;
                        }
                        else if (newprice.ValidityOTFL == "")
                        {
                            price.ValidityOTFL = newprice.ValidityOTFL;
                            ischanged = true;
                        }

                        newprice.DescriptionOTFL = HttpUtility.HtmlDecode(newprice.DescriptionOTFL);
                        if ((newprice.DescriptionOTFL != "") && (newprice.Description != price.DescriptionOTFL.Split('_')[0]))
                        {
                            newEx = ex + "_" + price.DescriptionOTFL.Split('_')[0];
                            if (price.DescriptionOTFL.Split('_').Length >= 2)
                                newEx = ex + "_" + price.DescriptionOTFL.Split('_')[1].Substring(0, price.DescriptionOTFL.Split('_')[1].Length) + " " + price.DescriptionOTFL.Split('_')[0];
                            price.DescriptionOTFL = newprice.DescriptionOTFL + newEx;
                            ischanged = true;
                        }
                        else if (newprice.DescriptionOTFL == "")
                        {
                            price.DescriptionOTFL = newprice.DescriptionOTFL;
                            ischanged = true;
                        }
                        price.UserID = long.Parse(context.Session["logined_id"].ToString());
                        if (ischanged)
                        {
                            price.LastUpdate = DateTime.Now;
                            //Update Log                                                                                
                            price.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                        }
                        i++;
                    }
                }
            }
            var t = new { UserName = context.Session["logined_user"], Date = DateTime.Now.ToString("dd-MM-yyyy") };
            context.Response.Write(JsonConvert.SerializeObject(t));
        }


    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}